package com.brillilab.starter.controller.aio.system;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.brillilab.common.alioss.AliSts;
import com.brillilab.common.alioss.OssFileEnum;
import com.brillilab.common.alioss.OssUtils;
import com.brillilab.common.constant.ClientTypeEnum;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.enums.file.FileDownloadTypeEnum;
import com.brillilab.domain.po.experiments.Experiment;
import com.brillilab.domain.po.system.FileManage;
import com.brillilab.domain.po.topics.Topic;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.experiments.IExperimentService;
import com.brillilab.service.core.system.IFileDownloadService;
import com.brillilab.service.core.system.IFileManageService;
import com.brillilab.service.core.topics.ITopicService;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.constant.StarterConstant;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 文件下载
 * 
 * @author LJ
 *
 */
@RestController
@RequestMapping("/api/file/download")
public class FileDownloadController {
	@Resource
	private IFileManageService fileManageService;

	@Resource
	private IFileDownloadService fileDownloadService;

	@Resource
	private IExperimentService experimentService;
	@Resource
	private ITopicService topicService;
	 
	@Resource
	private UsersLogic usersLogic;

	@Resource
    private OssUtils ossUtil;
	
	/**
	 * 用户下载回调
	 * 
	 * @return
	 */
	@GetMapping("/userCallback")
	public ResponseVo userCallback(@RequestParam Long fileId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		FileManage fileManage = fileManageService.selectById(fileId);
		if (fileManage != null && (OssFileEnum.AnalyzeDataFile.getKey().equals(fileManage.getAttachType())
				|| OssFileEnum.AnalyzeDataFileThumbnail.getKey().equals(fileManage.getAttachType())
				|| OssFileEnum.PrimaryDataFile.getKey().equals(fileManage.getAttachType()))) {

			FileDownloadTypeEnum downloadType = null;
			if (StrUtil.containsAny(token, ClientTypeEnum.PC.getValue())) {
				downloadType = FileDownloadTypeEnum.PC;
			} else if (StrUtil.containsAny(token, ClientTypeEnum.IOS.getValue(), ClientTypeEnum.ANDROID.getValue())) {
				downloadType = FileDownloadTypeEnum.APP;
			}
			if (downloadType != null)
				fileDownloadService.save(fileId,fileManage.getAttachType(), userInfo.getLabId(), userInfo.getLabMemberId(), downloadType);
		}
		return ResponseVo.success();
	}

	/**
	 * 一体机下载回调
	 * 
	 * @return
	 */
	@GetMapping("/aioCallback")
	public ResponseVo aioCallback(@RequestParam Long fileId, @RequestParam Long labId) {
		FileManage fileManage = fileManageService.selectById(fileId);
		if (fileManage != null && (OssFileEnum.AnalyzeDataFile.getKey().equals(fileManage.getAttachType())
				|| OssFileEnum.AnalyzeDataFileThumbnail.getKey().equals(fileManage.getAttachType())
				|| OssFileEnum.PrimaryDataFile.getKey().equals(fileManage.getAttachType()))) {
			fileDownloadService.save(fileId, fileManage.getAttachType(), labId, 0L, FileDownloadTypeEnum.AIO);
		}
		return ResponseVo.success();
	}

	/**
	 * 获取文件ID列表
	 * 
	 * @param type     类型：1-课题
	 * @param sourceId
	 * @return
	 */
	@GetMapping("/fileIds")
	public ResponseVo getFileIds(@RequestParam Integer type, @RequestParam Long sourceId) {
		List<Long> deleteList = fileDownloadService.getFileIds(type, sourceId);
		return ResponseVo.success(deleteList);
	}
	
	/**
	 * 获取本地上传文件列表 
	 * @param token
	 * @return
	 */
	@GetMapping("/localUploadFileList")
	public ResponseVo downloadFileList(@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		List<FileManage> fileList = CollUtil.toList();
				//fileDownloadService.getDownloadFileList(userInfo.getId(), "2019-11-13 00:00:00","2019-11-23 23:59:59");
		return ResponseVo.success(fileList);
	}
	
	/**
	 * 本地上传成功回调
	 * @param fileId
	 * @return
	 */
	@GetMapping("/localUploadCallback")
	public ResponseVo resetIsDownload(@RequestParam Long fileId) {
		FileManage fileManage = fileManageService.selectById(fileId);
//		if(fileManage!=null && fileManage.getIsDownload().equals(BoolEnum.TRUE.getValue())) {
		if(fileManage!=null) {
//			fileManage.setIsDownload(BoolEnum.FALSE.getValue());
			fileManage.setCallbackTime(new Date());
			fileManage.setCreateTime(new Date());
			fileManageService.updateById(fileManage);
		}
		return ResponseVo.success();
	}
	
	/**
	 * 本地上传权限OSS参数 
	 * @param fileId
	 * @return
	 */
	@GetMapping("/localUploadParam")
	public ResponseVo uploadParam(@RequestParam Long fileId) {
		FileManage fileManage = fileManageService.selectById(fileId);
		Experiment experiment = experimentService.selectById(fileManage.getReferDataId());
		Topic topic = topicService.selectById(experiment.getTopicId());
		
		Map<String, Object> resultMap = new HashMap<>();
		AliSts sts = ossUtil.getThirdAppUploadPermission();
		resultMap.put("accessKeyId", sts.getAccessKeyId());
		resultMap.put("accessKeySecret", sts.getAccessKeySecret());
		resultMap.put("securityToken", sts.getSecurityToken());
		resultMap.put("expireTime", sts.getExpireTime());
		resultMap.put("bucketName", fileManage.getBucketName());
		resultMap.put("basePath", fileManage.getPath());
		resultMap.put("deskey", topic.getFileSecretKey());
		return ResponseVo.success(resultMap);
	}
	
}
